﻿Imports System.Collections.Generic
Imports EnvDTE
Imports System.Linq


Public MustInherit Class BaseProvider

  Public MustOverride Sub Run(app As EnvDTE80.DTE2)

  Protected Function ExtractProjectHolders(projects As EnvDTE.Projects) As ProjectHolderCollection
    Dim projs = From lq In projects Select CType(lq, Project)
    Dim lstProjs = projs.ToList()
    Dim phs = GetProjectHolders(lstProjs)
    phs.Sort()
    Return phs
  End Function

  Protected Function GetProjectHolders(projects As List(Of Project), Optional upperPath As String = "") As ProjectHolderCollection
    Dim ret As New ProjectHolderCollection

    For Each p As Project In projects
      If p Is Nothing Then Continue For
      Dim fullName = TryGetFullName(p)
      If fullName Is Nothing Then Continue For

      If fullName = "" Then

        Dim projs = From lq In p.ProjectItems Select CType(lq, ProjectItem).SubProject
        Dim lstProjs = projs.ToList()

        Dim pomUpperPath As String
        If (String.IsNullOrEmpty(upperPath)) Then
          pomUpperPath = p.Name
        Else
          pomUpperPath = System.IO.Path.Combine(upperPath, p.Name)
        End If
        Dim pom = GetProjectHolders(lstProjs, pomUpperPath)

        ret.AddRange(pom)

      Else
        Dim ph = New ProjectHolder()
        ph.Project = p
        ph.UpperPath = upperPath
        ret.Add(ph)
      End If
    Next

    Return ret

  End Function

  Protected Function TryGetFullName(project As Project) As String
    Dim ret As String
    Try
      ret = project.FullName
    Catch ex As Exception
      ret = Nothing
    End Try
    Return ret
  End Function

  Protected Function SelectSingleProject(projects As ProjectHolderCollection, questionTitle As String) As ProjectHolder

    Dim f As New FrmSelectSingleProject
    f.InitWithProjects(projects, questionTitle)

    f.ShowDialog()

    If (f.DialogResult <> DialogResult.OK) Then
      Return Nothing
    Else
      Return f.SelectedProject
    End If

  End Function

  Protected Function SelectMultipleProjects(projects As ProjectHolderCollection, questionTitle As String) As ProjectHolderCollection

    Dim f As New FrmSelectMultipleProjects

    f.InitWithProjects(projects, questionTitle)

    f.ShowDialog()

    If f.DialogResult <> DialogResult.OK Then
      Return Nothing
    Else
      Return f.SelectedProjects
    End If

  End Function

  Protected Function GetCopyProviderSettings() As CopyProvider.CopyProviderSettings
    Dim dr = _
      MessageBox.Show("Would you also remove links from target project to files not existing in source project? (This is UNDONE and may DELETE some required links.)", _
                       "Settings...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

    Dim ret As New CopyProvider.CopyProviderSettings

    ret.RemoveNonSourceLinks = dr = DialogResult.Yes

    Return ret
  End Function

End Class
